package com.jiang.controller;

import com.jiang.anno.Log;
import com.jiang.pojo.Dept;
import com.jiang.pojo.Result;
import com.jiang.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired // 面向接口编程 注入service
    private DeptService deptService;

    // @RequestMapping(value = "/depts",method = RequestMethod.GET) // 指定请求方式为GET
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        // 调用service查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门{}",id);
        deptService.delete(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：" + dept.getName());
        deptService.add(dept);
        return Result.success();
    }
    /*
    * 根据ID查询部门
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询ID为：{}的部门",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @Log
    /*根据ID修改部门信息*/
    @PutMapping
    public Result getByIdUpdate(@RequestBody Dept dept){
        log.info("修改ID为：{}的部门",dept.getId());
        deptService.getByIdUpdate(dept);
        return Result.success();
    }


}
